/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package domain;


/**
 *
 * @author Oriol
 */
import data.FileControllerD;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.StringTokenizer;

public class VerbDictionary {

    private Trie<String> _vTrie;
    private BufferedReader _input;
    private FileControllerD _fc;
    private String _sep = System.getProperty("file.separator");
	private String _path;



    public VerbDictionary()  {
        _vTrie = new Trie<String>();
		
		
		/*_actualUser = actualUser;

		
		String jarPath = System.getProperty("java.class.path");
		int lastSlash = jarPath.lastIndexOf(_sep);
		_path = jarPath.substring(0,lastSlash + 1);
		


		//_path = "."+_sep;

		
		//loadUserDict();
        */

    }
    public void inicialize(BufferedReader verbs, BufferedReader irregulars, BufferedReader added) {
        createDict(verbs, irregulars);
		if (added != null) {
			loadUserDict(added);
		}

    }

    private void createDict(BufferedReader verbs, BufferedReader irregulars)  {
        String _infinitive = null;
		String _line = null;
            try {
                _infinitive = verbs.readLine();
            } catch (IOException ex) {
                //Logger.getLogger(VerbDictionary.class.getName()).log(Level.SEVERE, null, ex);
            }
         
            while(_infinitive != null) {
                includeVerb(_infinitive);
                try {
                    _infinitive = verbs.readLine();
                } catch (IOException ex) {
                    //Logger.getLogger(VerbDictionary.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

			StringTokenizer tokens = null;
			
           

            try {
                _line = irregulars.readLine();
            } catch (IOException ex) {
                //Logger.getLogger(VerbDictionary.class.getName()).log(Level.SEVERE, null, ex);
            }
         
            while(_line != null) {
				tokens = new StringTokenizer(_line);
				_infinitive = tokens.nextToken();
				while(tokens.hasMoreTokens()) {
					String _irregular = tokens.nextToken();
					_vTrie.put(_irregular, _infinitive);
				}
                
                try {
                    _line = irregulars.readLine();
                } catch (IOException ex) {
                    Logger.getLogger(VerbDictionary.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

       

    }
	/**
	*
	* Escriu per consola tots els infinitius associats del diccionari.
	 * Hi ha repetits.
	*
	* @author oriol.cano
	* @param -
	* @return -
	* @throws -
	*/
    public void writeDict() {
        ArrayList<String> verbs =_vTrie.getAll();
        for(String verb: verbs){
            System.out.println(verb);
        }
    }
	/**
	*
	*
	* Obté l'infinitiu associat a una forma verbal.
	*
	* @author oriol.cano
	* @param String verb: Forma verbal de la que vols aconseguir l'infinitiu.
	* @return Infinitiu associat a la forma verbal que se li passa com a paràmetre.
	 * Retorna null si el paràmetre d'entrada no es una forma verbal correcte.
	* @throws -
	*/
    public String getInfinitive(String verb) {
        String _ret = _vTrie.get(verb);
        return _ret;
    }
	/**
	*
	*
	* Afegeix un verb nou al diccionari. El nou verb serà conjugat com si fos regular.
	*
	* @author oriol.cano
	* @param String s: Verb que es vol afegir al diccionari.
	* @return -
	* @throws -
	*/
    public void addVerbToDict(String s) {

        includeVerb(s);
       /*try {

        String user = _actualUser+"";
        _fc = new FileControllerD();
       _output = _fc.getFileW(_path+"users"+_sep+user+_sep+"added.txt");
  
       _output.append(s);
       _output.newLine();
       _output.close();
       includeVerb(s);

       
       }
       catch(Exception e) {
           String user = "1";
           File aux = new File(_path+"users"+_sep+user);
		   boolean b = aux.mkdir();
		   File _file = new File(_path+"users"+_sep+user+_sep+"added.txt");

            try {
                b = _file.createNewFile();
                _output = _fc.getFileW(_path+"users"+_sep+user+_sep+"added.txt");
                
                _output.append(s);
                _output.newLine();
                _output.close();
                 includeVerb(s);
            } catch (IOException ex) {
                Logger.getLogger(VerbDictionary.class.getName()).log(Level.SEVERE, null, ex);
            }
           //_file = _fc.getFile("."+_sep+"users"+_sep+user+_sep+"added.txt");



        //Logger.getLogger(VerbDictionary.class.getName()).log(Level.SEVERE, null, e);
       }
*/
    }

    private void includeVerb(String s) {
        int l = s.length() ;
        String last = s.substring(l-1);

        String aux = s;
		String aux2 = null;
		String aux3 = null;
		
         _vTrie.put(aux, (String)s);
		 _vTrie.put(aux+"d", (String)s);
         aux = s;
         _vTrie.put(aux+"ed", (String)s);
         aux = s;
         _vTrie.put(aux+"s", (String)s);
         aux = s;
         _vTrie.put(aux+"es", (String)s);
         aux = s;
         _vTrie.put(aux+last+"ing", (String)s);
		 
		 aux2 = s.substring(l-2);
		 if (aux2.equals("ie")) {
			aux3 = s.substring(0,l-2);
			_vTrie.put(aux3+"ying", (String)s);
	
		 }else {
			_vTrie.put(aux+"ing", (String)s);
			aux = s;
		 }
		 if(last.equals("y")) {
			 aux3 = s.substring(0,l-1);
			 _vTrie.put(aux3+"ies", (String)s);
			 _vTrie.put(aux3+"ied", (String)s);
		 }
		 if(last.equals("e")) {
			 aux3 = s.substring(0,l-1);
			 _vTrie.put(aux3+"ing", (String)s);

		 }

    }

    private void loadUserDict(BufferedReader input) {
    
        String _infinitive = null;
     
                  _input = input;
           
            try {
                _infinitive = _input.readLine();
            } catch (IOException ex) {
                Logger.getLogger(VerbDictionary.class.getName()).log(Level.SEVERE, null, ex);
            }

            while(_infinitive != null) {
                includeVerb(_infinitive);
                try {
                    _infinitive = _input.readLine();
                } catch (IOException ex) {
                    //Logger.getLogger(VerbDictionary.class.getName()).log(Level.SEVERE, null, ex);
                }
            }            
      

    }





}
